package com.anXin.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.anXin.apis.equipment.IEquipmentClient;
import com.anXin.model.common.dtos.ResponseResult;
import com.anXin.model.common.enums.AppHttpCodeEnum;
import com.anXin.model.equipment.Equipment;
import com.anXin.model.user.dtos.EquipmentInfoDto;
import com.anXin.model.user.pojos.Oldman;
import com.anXin.model.user.pojos.UserEquipment;
import com.anXin.user.mapper.UserEquipmentMapper;
import com.anXin.user.service.OldmanService;
import com.anXin.user.service.UserEquipmentService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import utils.thread.AppThreadLocalUtil;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;


@Service("userEquipmentService")
public class UserEquipmentServiceImpl extends ServiceImpl<UserEquipmentMapper, UserEquipment> implements UserEquipmentService {

    @Resource
    private IEquipmentClient equipmentClient;

    @Resource
    private OldmanService oldmanService;

    @Override
    public ResponseResult add(UserEquipment userEquipment) {
        userEquipment.setUserId(AppThreadLocalUtil.getUser().getId());
//        UserEquipment one = query().eq("mac", userEquipment.getMac()).one();
//        if (one!=null)return ResponseResult.errorResult(AppHttpCodeEnum.USER_EQUIMENT_EXIST);
//        ResponseResult equipmentResult = equipmentClient.getEquipment();
//        if (equipmentResult.getCode()==503)return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"获取数据失败");
//        LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) equipmentResult.getData();
//        userEquipment.setVision((String) data.get("deviceVersion"));
//        userEquipment.setMac((String) data.get("mac"));
        userEquipment.setMac("EAA29AE7BEEE");
        userEquipment.setVision("Surge");
        boolean save = save(userEquipment);

        if (save){
            UserEquipment equipment = query().eq("mac",userEquipment.getMac()).eq("oldman_id", userEquipment.getOldmanId()).eq("user_id", AppThreadLocalUtil.getUser().getId()).one();
            Oldman oldman = oldmanService.query().eq("id", userEquipment.getOldmanId()).one();
            oldman.setEquipment(equipment.getId());
            boolean update = oldmanService.updateById(oldman);
            if (update){
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }

        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    @Override
    public ResponseResult updateEquipment(UserEquipment userEquipment) {
        boolean updateById = updateById(userEquipment);
        if (updateById)return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    @Override
    public ResponseResult delete(Long id) {
        boolean removeById = removeById(id);
        if (removeById){
            Oldman one = oldmanService.query().eq("equipment", id).eq("user_id", AppThreadLocalUtil.getUser().getId()).one();
            one.setEquipment(null);
            boolean b = oldmanService.updateById(one);
            if (b) return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    @Override
    public ResponseResult getList(Long id) {
        List<UserEquipment> list = query().eq("oldman_id", id).eq("user_id", AppThreadLocalUtil.getUser().getId()).list();
        if (list.isEmpty())return ResponseResult.errorResult(AppHttpCodeEnum.DATA_LIST_NULL);
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult Info(Long id) {
        UserEquipment userEquipment = query().eq("id", id).one();
        EquipmentInfoDto equipmentInfoDto = BeanUtil.copyProperties(userEquipment, EquipmentInfoDto.class);
        LinkedHashMap<String,Object> equipment = (LinkedHashMap<String,Object>) equipmentClient.getEquipment().getData();
        equipmentInfoDto.setBattery((String) equipment.get("battery"));
        equipmentInfoDto.setBatteryLevel((Integer) equipment.get("batteryLevel"));
        equipmentInfoDto.setLastSyncTime((String) equipment.get("lastSyncTime"));
        return ResponseResult.okResult(equipmentInfoDto);
    }


}